import wx
from Constant import *
'''
GameObj_StoneWall_Turnable :

    Game object of turnable stone wall. When it first created, it likes a stone wall. However, once a object
    
    or diamond falls to up of the turnable wall, the effect starts, and the falling object will be turned to
    
    the another type of object at down of wall in the period of lasting time of effect. Stone will be 
    
    changed to diamond, and vice versa. But something user should pay attention to, the effect can be only
    
    started up one time, once the effect disappear, the object will be turn to nothing.
    
'''
class GameObj_StoneWall_Turnable():
    '''
    Constructor : GameObj_StoneWall_Turnable
        mainMap - object pointer MainMap
        posInMap - the position in map (row, column)
        lastTime -  the lasting time of specific effect
    '''
    def __init__(self, mainMap, posInMap, lastTime):
        self._001_initialInstanceVariable(mainMap, posInMap, lastTime)
        self._002_bindAction()
    '''
    function _001 : initialize the variables used for this object
        mainMap - object pointer MainMap
        posInMap - the position in map (row, column)
        lastTime -  the lasting time of specific effect
    '''
    def _001_initialInstanceVariable(self, mainMap, posInMap, lastTime):
        #object pointer - MainMap
        self.mainMap = mainMap
        #record current position in map (row, column)
        self.posInMap = posInMap
        #record the lasting time of specific effect
        self.effectLastTime = lastTime
        #record the current lasting time of specific effect
        self.curTime = 0
        #current image index of this
        self.curImageIndex = 0
        #record the effect is starting
        self.isStart = False
        #record the effect is starting
        self.hasEverStart = False
        #timer of lasting specific effect
        self.timer_effect = wx.Timer()
    '''
    function _002 : bind object event to specific method
    '''
    def _002_bindAction(self):
        self.timer_effect.Bind(wx.EVT_TIMER, self.SLOT001_lastingEffect)
        self.mainMap.evtCollector.Bind(EVT_DESTROY_GAME_OBJ, self.SLOT002_destroy)
        self.mainMap.evtCollector.Bind(EVT_HIT_TURN_STONE_WALL, self.SLOT003_generateProduct)
    '''
    function _003 : destroy all object of this class
    ''' 
    def _003_destroyAll(self):
        self.timer_effect.Stop()
        self.timer_effect.Destroy()
        del self
    '''
    function _004 : when the effect starts up, it will call signal of starting effect to 
                    other turnable wall around this object
    '''
    def _004_chainEffect(self):
        dir = [[0,1], [0,-1], [1,0], [-1,0]]
        index = 0
        while index < dir.__len__() :
            newPos = (self.posInMap[0] + dir[index][0], self.posInMap[1] + dir[index][1])
            if self.mainMap.GET001_objTypeIn_listCube(newPos) == GAME_OBJ_STONE_WALL_TURNABLE :
                wx.PostEvent(self.mainMap.evtCollector, NewEvt_HitTurnableStoneWall(pos = newPos, callObj = None, isGenProduct = False))
            index = index + 1
    '''
    function SLOT001 : the slot of effect lasting
    '''
    def SLOT001_lastingEffect(self, event):
        self.curImageIndex = (self.curImageIndex + 1) % LIST_FILENAME_GAMEOBJECT[GAME_OBJ_STONE_WALL_TURNABLE].__len__()
        self.mainMap.ACT003_changeObj(self.posInMap, GAME_OBJ_STONE_WALL_TURNABLE, self.curImageIndex)
        self.curTime = self.curTime + 1
        if self.curTime >= self.effectLastTime :  #stop effect lasting
            self.isStart = False
            self.timer_effect.Stop()
            self.mainMap.ACT003_changeObj(self.posInMap, GAME_OBJ_STONE_WALL_TURNABLE, 0)
    '''
    function SLOT002 : the slot of effect destroying
    '''
    def SLOT002_destroy(self, event):
        if not event.isClearAll :
            if event.delObjType == GAME_OBJ_STONE_WALL_TURNABLE and self.posInMap[0] == event.delPos[0] and \
            self.posInMap[1] == event.delPos[1] :
                self._003_destroyAll()
        else :
            self._003_destroyAll()
        event.Skip()
    '''
    function SLOT002 : the slot of generating the product at down of this
    '''
    def SLOT003_generateProduct(self, event):
        if event.pos[0] == self.posInMap[0] and event.pos[1] == self.posInMap[1] :
            if not self.hasEverStart :  #start the special effect
                self.hasEverStart = True
                self.isStart = True
                self._004_chainEffect()
                self.timer_effect.Start(1000)
            if (self.isStart) and (event.isGenProduct):  #generate the product down of this
                pos_gen = (self.posInMap[0] + 1, self.posInMap[1])
                objType = self.mainMap.GET001_objTypeIn_listCube(pos_gen)
                if objType == GAME_OBJ_BLACK :  #there is no obstacle at down
                    obj_callGen = event.callObj
                    if obj_callGen == GAME_OBJ_STONE :  #turn stone to diamond
                        self.mainMap.ACT003_changeObj(pos_gen, GAME_OBJ_DIAMOND, 0)
                        self.mainMap.ACT006_createGameObj(GAME_OBJ_DIAMOND, pos_gen)
                    elif obj_callGen == GAME_OBJ_DIAMOND :  #turn diamond to stone
                        self.mainMap.ACT003_changeObj(pos_gen, GAME_OBJ_STONE, 0)
                        self.mainMap.ACT006_createGameObj(GAME_OBJ_STONE, pos_gen)
        event.Skip()